#!/bin/bash
# Filename: bond.sh
# a shell script for create bond device, for redflag-linux only.

BONDDEVICE=$1
SLAVE1=$2
SLAVE2=$3
IPADDR=$4
NETMASK=$5
GATEWAY=$6
USER_EXIT_CODE=60
PARAM_EXIT_CODE=61
BONDTYPE=$7
BONDMODE=$8
MODCONF=/etc/modprobe.conf

MODIFY_NORMAL_MASTER(){
    echo -e "DEVICE=$BONDDEVICE \
        \nIPADDR=$IPADDR \
        \nNETMASK=$NETMASK \
        \nGATEWAY=$GATEWAY \
        \nONBOOT=yes \
        \nBOOTPROTO=none \
        \nUSERCTL=no" > /etc/sysconfig/network-scripts/ifcfg-$BONDDEVICE
}

MODIFY_NORMAL_SLAVE(){
    echo -e "DEVICE=$1 \
        \nHWADDR=`ifconfig $1 | grep HWaddr | awk '{print $5}'` \
        \nUSERCTL=no \
        \nONBOOT=yes \
        \nMASTER=$BONDDEVICE \
        \nSLAVE=yes \
        \nBOOTPROTO=none" > /etc/sysconfig/network-scripts/ifcfg-$1
}

MODIFY_INFINIBAND_MASTER(){
    echo -e "DEVICE=$BONDDEVICE \
        \nIPADDR=$IPADDR \
        \nNETMASK=$NETMASK \
        \nGATEWAY=$GATEWAY \
        \nONBOOT=yes \
        \nBOOTPROTO=none \
        \nUSERCTL=no" > /etc/sysconfig/network-scripts/ifcfg-$BONDDEVICE
}

MODIFY_INFINIBAND_SLAVE(){
    echo -e "DEVICE=$1 \
        \nUSERCTL=no \
        \nONBOOT=yes \
        \nMASTER=$BONDDEVICE \
        \nSLAVE=yes \
        \nBOOTPROTO=none \
        \nTYPE=InfiniBand" > /etc/sysconfig/network-scripts/ifcfg-$1
}

# Run this script as root
if [ $UID != 0 ]; then
	echo "Please Run $0 As root!!"
	exit $USER_EXIT_CODE
fi

# check the parameter
if [ $# -lt 6 ]; then
    echo -e "Usage: $0 <bond*> <eth*> <eth*> <ipaddress> <netmask> <gateway> <bondtype> <bondmode> \
        \n \
        \neg: for normal type: $0 bond0 eth0 eth1 10.0.0.1 255.255.255.0 10.0.0.254 0 1 \
        \n    for infiband type: $0 bond0 ib0 ib1 10.0.0.1 255.255.255.0 10.0.0.254 1 1 \
        \n \
        \nbondtype: [0/1], default is 0, 0 for normal type, 1 for infiniband type \
        \nbondmode: [0-6], default is 1, 0 for balance-rr, 1 for active-backup, 2 for balance-xor, \
        \n          3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, 6 for balance-alb (charp)"
	exit $PARAM_EXIT_CODE
fi

# your bond type?
if [ "$BONDTYPE" = "1" ] || [ "$BONDTYPE" = "infiniband" ] || echo $@ | grep "ib" > /dev/null 2>&1; then
    BONDMODE=1
	MODIFY_INFINIBAND_MASTER
	MODIFY_INFINIBAND_SLAVE $SLAVE1
	MODIFY_INFINIBAND_SLAVE $SLAVE2
else
	MODIFY_NORMAL_MASTER
	MODIFY_NORMAL_SLAVE $SLAVE1
	MODIFY_NORMAL_SLAVE $SLAVE2
fi

# modify modprobe.conf

BACKFILE=/etc/.modprobe.conf.`date +%Y-%m-%d`.$RANDOM.bak
TEMPFILE1=`mktemp`
TEMPFILE2=`mktemp`

cp $MODCONF $BACKFILE
if ! grep 'alias.*bond[0-9].*bonding' $MODCONF > /dev/null 2>&1; then
    if ! grep 'modprobe.*bonding.*bond' $MODCONF > /dev/null 2>&1; then
        echo -e "alias $BONDDEVICE bonding \noptions $BONDDEVICE miimon=100 mode=${BONDMODE:=1}" >> $MODCONF
    else
        echo "install $BONDDEVICE /sbin/modprobe --ignore-install bonding -o $BONDDEVICE miimon=100 mode=${BONDMODE:=1}" >> $MODCONF
    fi
else
    ORIBONDDEV=`grep 'alias.*bond[0-9]' $MODCONF | awk '{print $2}'`
    ORIBONDMODE=`grep 'options.*bond[0-9]' $MODCONF | awk '{print $4}'`
    sed '/alias.*bond[0-9]/d;/options.*bond/d' $MODCONF > $TEMPFILE1
    if [ -z $ORIBONDDEV -a -z $ORIBONDMODE ]; then
        echo "install $BONDDEVICE /sbin/modprobe --ignore-install bonding -o $BONDDEVICE miimon=100 mode=${BONDMODE:=1}" >> $TEMPFILE1
        mv -f $TEMPFILE1 $MODCONF
        rm -f $TEMPFILE1
    else
        echo "install $ORIBONDDEV /sbin/modprobe --ignore-install bonding -o $ORIBONDDEV miimon=100 $ORIBONDMODE" >> $TEMPFILE1
	echo "install $BONDDEVICE /sbin/modprobe --ignore-install bonding -o $BONDDEVICE miimon=100 mode=${BONDMODE:=1}" >> $TEMPFILE1
        mv -f $TEMPFILE1 $MODCONF
        rm -f $TEMPFILE1
    fi
fi
    
echo "The configuration was successed, Please restart your network."

# restart network
echo "System will restart network continue(y/n)?"
read OPS
if [ "$OPS" = "y" -o "$OPS" = "yes" -o "$OPS" = "Y" ]; then
    for tempmod in $(lsmod | grep -i bond | awk '{print $1}')
    do
        modprobe -r bonding -o "$tempmod"
    done
    /etc/init.d/network restart
fi

exit 0
